package com.dp;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution31 {
    public int rob(TreeNode root) {
        int[] res = robAct(root);
        return Math.max(res[0],res[1]);
    }

    int[] robAct(TreeNode root) {
        int[] res = new int[2];
        if (root == null) return res;

        int[] left = robAct(root.left);
        int[] right = robAct(root.right);

        res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        res[1] = root.val + left[0] + right[0];
        return res;
    }
}
